<?php


namespace App\Console\Commands;


use App\Modules\Order\Models\Order;
use App\Modules\User\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;

class HourPush24 extends Command
{
    /**
     * console命令的名称和签名。
     *
     * @var string
     */
    protected $signature = 'HourPush24';

    /**
     * console命令说明。
     *
     * @var string
     */
    protected $description = '用于确认收货之后24小时后未评价的提醒';

    /**
     * 创建一个新的命令实例。
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     *  临洮：订单超时未评价
     *  模板ID：ufJlNLyyoeMyqbRlyYRb40RtbCfOejqrkzUUQqTBu8w
     *  格式：
     * {{first.DATA}} //订单超时未支付
     * 订单号：{{keyword1.DATA}}
     * 创建时间：{{keyword2.DATA}}
     * 商品明细：{{keyword3.DATA}}
     * {{remark.DATA}} //请您及时付款以免过期。
     */
    public function handle()
    {
        $yesterday = date("Y-m-d 00:00:00", strtotime("-2 day"));
        $current = date("Y-m-d H:i:s");
        $order = Order::query()
            ->where([
                ["created_at", '>', $yesterday],
                ["status", Order::STATUS_PAID],
                ["is_finish", 1],
                ["comment_remind", 0],
                ["is_comment", 0],
            ])
            ->get();
        foreach ($order as $o) {
            $MinutesLater30 = date('Y-m-d H-i-s', strtotime("+1 day", strtotime($o->created_at)));
            $systemUser = User::query()
                ->whereRaw("JSON_CONTAINS(shop_id, '\"" . $order->shop_id . "\"')")
                ->where("is_palm_management", 1)
                ->get(["openid","is_palm_management"])->toArray();

            if ($MinutesLater30 <= $current) {
                $user = User::query()->where("id", $o->user_id)->first();
                if ($user) {
                    $sendData = [$user->openid, $o->id, date("Y年m月d日 H时i分s秒")];
                    if ($order->delivery == 0 && $order->is_prepay == 1) { //到店自取-定金支付
                        sendMessage(24, $sendData);
                        sendSystem($systemUser, 46, $sendData);
                    } elseif ($order->delivery == 0 && $order->is_prepay == 0) {//到店自取-全款支付
                        sendMessage(25, $sendData);
                        sendSystem($systemUser, 47, $sendData);
                    } elseif ($order->delivery > 0 && $order->is_prepay == 1) {//物流发货-定金支付
                        sendMessage(26, $sendData);
                        sendSystem($systemUser, 48, $sendData);
                    } elseif ($order->delivery > 0 && $order->is_prepay == 0) {//物流发货-全款支付
                        sendMessage(27, $sendData);
                        sendSystem($systemUser, 49, $sendData);
                    }

                    Log::channel('console')->info("24hpush", $sendData);
                    Order::query()->where("id", $o->id)->update(["comment_remind" => 1]);
                }
            }
        }
    }
}
